home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
5
/
CRON_1
/
CRON_SET
/
CRON
/
ARGC_REC.C
< prev
next >
Wrap
Text File
|
1991-09-22
|
7KB
|
320 lines
/* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
Filename: "argc Receiver.c"
argc/argv argument receiver code, written for THINK C 5.0
By Chris Johnson
Version of: Sunday, September 15, 1991 7:00 PM
Distribute freely and without charge, but say something nice about
the author when you use it. Please send me a copy of any improve-
ments you make so they can be incorporated into future versions.
╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
Internet: chrisj@emx.utexas.edu
UUCP: {husc6|uunet}!cs.utexas.edu!ut-emx!chrisj
BitNet: chrisj@utxvm.bitnet
AppleLink: chrisj@emx.utexas.edu@internet#
CompuServe: >INTERNET:chrisj@emx.utexas.edu
US Mail: Chris Johnson, 3311 Red River #305, Austin, TX 78705
╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ */
#include "argc Receiver.h"
#include <AppleEvents.h>
#include <GestaltEqu.h>
#define argcEventClass 'args'
#define argcEventID 'argc'
#define argcKeyword 'argc'
#define LBitMask(BitNum) (0x1L << (BitNum))
#define _WaitNextEvent 0xA860
#define _Gestalt 0xA1AD
static Boolean WaitFlag;
static int argc;
static unsigned char **argv;
Boolean EnvironmentCheck(void);
pascal OSErr Quit(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
pascal OSErr ArgsGet(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
void ArgsDispose(void);
OSErr MyGotRequiredParams(AppleEvent *Event);
void HandleHighLevelEvent(EventRecord *Event);
int NumToolboxTraps(void);
TrapType GetTrapType(int theTrap);
Boolean TrapAvailable(int theTrap);
void argcReceiver(argcRcvd, argvRcvd)
int *argcRcvd;
char ***argvRcvd;
{
ArgsDispose();
if (EnvironmentCheck()) {
unsigned long TimeoutTicks;
TimeoutTicks = Ticks + 60 * 60;
while (WaitFlag) {
EventRecord Event;
if (WaitNextEvent(everyEvent, &Event, 60, NULL)) {
switch (Event.what) {
case keyDown:
WaitFlag = FALSE;
break;
case kHighLevelEvent:
AEProcessAppleEvent(&Event);
break;
}
}
// If we're still waiting and we've been waiting more than
// a minute so far it's time to quit.
if (Ticks >= TimeoutTicks)
WaitFlag = FALSE;
}
AERemoveEventHandler(kCoreEventClass, kAEQuitApplication, Quit, FALSE);
AERemoveEventHandler(argcEventClass, argcEventID, ArgsGet, FALSE);
}
if (argv == NULL)
ExitToShell();
*argcRcvd = argc;
*(unsigned char ***) argvRcvd = argv;
}
static Boolean EnvironmentCheck() {
Boolean Continue;
Continue = FALSE;
if (TrapAvailable(_WaitNextEvent)) {
if (TrapAvailable(_Gestalt)) {
long Response;
if (Gestalt(gestaltAppleEventsAttr, &Response) == noErr) {
if ((Response & LBitMask(gestaltAppleEventsPresent)) != 0) {
if (AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, Quit, 0, FALSE) == noErr) {
if (AEInstallEventHandler(argcEventClass, argcEventID, ArgsGet, 0, FALSE) == noErr) {
WaitFlag = TRUE;
Continue = TRUE;
}
}
}
}
}
}
return (Continue);
}
static pascal OSErr Quit(RcvdEvent, ReplyEvent, RefCon)
AppleEvent *RcvdEvent;
AppleEvent *ReplyEvent;
long RefCon;
{
OSErr OSError;
OSError = MyGotRequiredParams(RcvdEvent);
if (OSError == noErr)
WaitFlag = FALSE;
return (OSError);
}
static pascal OSErr ArgsGet(RcvdEvent, ReplyEvent, RefCon)
AppleEvent *RcvdEvent;
AppleEvent *ReplyEvent;
long RefCon;
{
OSErr OSError;
AEDesc ArgCList;
OSError = AEGetParamDesc(RcvdEvent, argcKeyword, typeAEList, &ArgCList);
if (OSError == noErr) {
long ArgCount;
OSError = AECountItems(&ArgCList, &ArgCount);
if (OSError == noErr) {
argc = ArgCount;
argv = (unsigned char **) NewPtrClear(sizeof(Ptr) * (ArgCount + 1));
OSError = MemError();
if (OSError == noErr) {
long Index;
unsigned char **CurArgV;
Index = 0;
CurArgV = argv;
while (--ArgCount >= 0 && OSError == noErr) {
DescType DataType;
long DataSize;
OSError = AESizeOfNthItem(&ArgCList, ++Index, &DataType, &DataSize);
if (OSError == noErr) {
*CurArgV = (unsigned char *) NewPtr(DataSize + 1);
OSError = MemError();
if (OSError == noErr) {
AEKeyword Keyword;
long ActualSize;
OSError = AEGetNthPtr(&ArgCList, Index, typeChar, &Keyword, &DataType, (Ptr) *CurArgV, DataSize, &ActualSize);
if (OSError == noErr)
*(*CurArgV + DataSize) = '\0';
}
}
CurArgV++;
}
}
}
AEDisposeDesc(&ArgCList);
}
if (OSError == noErr)
OSError = MyGotRequiredParams(RcvdEvent);
if (OSError != noErr) {
argc = 0;
argv = NULL;
}
WaitFlag = FALSE;
return (OSError);
}
static OSErr MyGotRequiredParams(Event)
AppleEvent *Event;
{
OSErr OSError;
DescType ReturnedType;
Size ActualSize;
OSError = AEGetAttributePtr(Event, keyMissedKeywordAttr, typeWildCard, &ReturnedType, NULL, 0, &ActualSize);
if (OSError == errAEDescNotFound) {
OSError = noErr;
} else if (OSError == noErr)
OSError = errAEEventNotHandled;
return (OSError);
}
static void ArgsDispose() {
if (argv != NULL) {
unsigned char **CurArg;
CurArg = argv;
while (--argc >= 0) {
if (*CurArg != NULL)
DisposPtr((Ptr) *CurArg);
CurArg++;
}
DisposPtr((Ptr) argv);
}
argc = 0;
argv = NULL;
}
/* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
All of the following code comes from IM VI pg. 3-8.
╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ */
#define _Unimplemented 0xA89F
#define _InitGraf 0xA86E
int NumToolboxTraps(void);
TrapType GetTrapType(int theTrap);
Boolean TrapAvailable(int theTrap);
static int NumToolboxTraps() {
int NumToolboxTraps;
if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
NumToolboxTraps = 0x200;
else
NumToolboxTraps = 0x400;
return (NumToolboxTraps);
}
static TrapType GetTrapType(theTrap)
int theTrap;
{
TrapType tType;
if ((theTrap & 0x0800) > 0)
tType = ToolTrap;
else
tType = OSTrap;
return (tType);
}
Boolean TrapAvailable(theTrap)
int theTrap;
{
TrapType tType;
tType = GetTrapType(theTrap);
if (tType == ToolTrap) {
theTrap &= 0x07FF;
if (theTrap >= NumToolboxTraps())
theTrap = _Unimplemented;
}
return (NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
}